﻿(function () {
    angular.module('KtotApp').service('TaskService', function () {
        var self = this;
        self.tasks = [
            {
                workItemId: 23,
                title: 'Criar Caso de Uso',
                iterationPath: 'Aquila/Sprint 10.0',
                userStory: 'Cadastro de Ocorrências',
                estimatedWork: 4,
                remainingWork: 1,
                completedWork: 3,
                totalWork: 3,
                totalWorkToday: 1
            },
            {
                workItemId: 23,
                title: 'Criar View',
                iterationPath: 'Aquila/Sprint 11.1',
                userStory: 'Cadastro de Ocorrências',
                estimatedWork: 4,
                remainingWork: 1,
                completedWork: 3,
                totalWork: 3,
                totalWorkToday: 1
            },
            {
                workItemId: 23,
                title: 'Criar Controller',
                iterationPath: 'Aquila/Sprint 11.1',
                userStory: 'Cadastro de Ocorrências',
                estimatedWork: 4,
                remainingWork: 1,
                completedWork: 3,
                totalWork: 3,
                totalWorkToday: 1
            },
            {
                workItemId: 23,
                title: 'Criar Service',
                iterationPath: 'Aquila/Sprint 11.1',
                userStory: 'Cadastro de Ocorrências',
                estimatedWork: 4,
                remainingWork: 1,
                completedWork: 3,
                totalWork: 3,
                totalWorkToday: 1
            },
            {
                workItemId: 23,
                title: 'Criar View Generica',
                iterationPath: 'Aquila/Sprint 11.1',
                userStory: 'Inbox de Ocorrência',
                estimatedWork: 4,
                remainingWork: 1,
                completedWork: 3,
                totalWork: 3,
                totalWorkToday: 1
            }
        ];

        var tasksGrouped = [];

        self.groupTasks = function (tasks) {            
            var byIterationPath = [];
            tasks.forEach(function (task, index) {
                var iteration = Enumerable.From(byIterationPath).FirstOrDefault(null, function (i) { return i.iterationPath == task.iterationPath; });
                if (!iteration) {
                    iteration = {
                        iterationPath: task.iterationPath,
                        isVisible: true,
                        userStories: []
                    };

                    byIterationPath.push(iteration);
                }

                var userStory = Enumerable.From(iteration.userStories).FirstOrDefault(null, function (i) { return i.userStory == task.userStory; });
                if (!userStory) {
                    userStory = {
                        userStory: task.userStory,
                        isVisible: true,
                        tasks: []
                    };

                    iteration.userStories.push(userStory);
                }

                userStory.tasks.push(task);
            });
            
            return byIterationPath;
        };

        self.getTasks = function () {
            return $.Deferred(function () {
                this.resolve(self.tasks);
            });
        };
    });
})();